import * as modInstances from "../module/index";

let NationalArray = ['汉族', '蒙古族', '回族', '藏族', '维吾尔族', '苗族', '彝族',
    '壮族', '布依族', '朝鲜族', '满族', '侗族', '瑶族', '白族', '土家族', ' 哈尼族',
    '哈萨克族', '傣族', '黎族', '僳僳族', '佤族', '畲族', '高山族', '拉祜族',
    '水族', '东乡族', '纳西族', '景颇族', '柯尔克孜族', '土族', '达斡尔族', '仫佬族',
    '羌族', '布朗族', '撒拉族', '毛难族', '仡佬族', '锡伯族', '阿昌族', '普米族',
    '塔吉克族', '怒族', '乌孜别克族', '俄罗斯族', '鄂温克族', '崩龙族', '保安族', '裕固族',
    '京族', '塔塔尔族', '独龙族', '鄂伦春族', '赫哲族', '门巴族', '珞巴族', '基诺族'
];
let re = new RegExp('(^\\s*)|(\\s*$)', 'g');

function handleNation(data) {
    let national = '';
    let nationalCode;

    if (NationalArray.includes(data.Nation + '族')) {
        national = `${data.Nation}族`;
        nationalCode = NationalArray.indexOf(data.Nation + '族');
    } else {
        nationalCode = parseInt((data.Nation).replace(re, ''));
        let nationalNum = nationalCode;
        if (nationalNum > 56 && nationalNum <= 0) {
            national = '其他';
        } else {
            national = NationalArray[nationalNum - 1];
        }
        console.log(national);
        data.Nation = national
    }
}

async function getIdCardState() {
    let { idcard } = modInstances
    let result = await idcard.GetState()
    return result
}

async function acceptAndReadTracks(trackMap = "3", timeout = -1) {
    let { idcard } = modInstances
    let result = await idcard.Insert(trackMap, timeout)
    return result.ret
}

let user = {
    name: null,
    sex: null,
    nationality: null,
    birthday: null,
    address: null,
    idcard: null,
    issue: null,
    startDate: null,
    endDate: null,
    passNumber: null,
    signTimes: null,
    certType: null,
}

async function getTrack3() {
    let { idcard } = modInstances

    let data = await idcard.GetTrack3();
    let typeCard = data.Type
    switch (typeCard) {
        case "0":
            handleNation(data)
            break;
        case "1":
            user.certType = "I"
            break;
        case "2":
            user.certType = "J"
            break;
    }
    console.log(data);
    user.name = data.Name;
    user.sex = data.Sex;
    user.nationality = data.Nation;
    user.birthday = data.Born;
    user.address = data.Address;
    user.idcard = data.IDCardNo;
    user.issue = data.GrantDept;
    user.startDate = data.UserLifeBegin;
    user.endDate = data.UserLifeEnd;
    user.passNumber = data.PassPortNo;
    user.signTimes = data.IssuesTimes
    return user
}

async function closeIDCardStep() {  
    let { idcard } = modInstances
    let result = await idcard.Close();
    return result.ret
}

export { getIdCardState, acceptAndReadTracks, getTrack3, closeIDCardStep }